package org.example.service.impl;

import org.example.Mapper.*;
import org.example.domain.User;
import org.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ApplyRecordMapper applyRecordMapper;

    @Autowired
    private ActivityRecordMapper activityRecordMapper;

    @Autowired
    private EmailRecordMapper emailRecordMapper;

    @Autowired
    private SummaryMapper summaryMapper;

    @Override
    public User login(User user) {
        return userMapper.login(user.getAccount());
    }

    @Override
    public Integer register(User user) {
        String[] accounts= userMapper.getAllAccount();
        boolean isIn=false;//学号是否已经被注册/申请中
        for (String account : accounts) {
            if(account.equals(user.getAccount())){
                isIn=true;
                break;
            }
        }
        if(isIn){
            User user1=userMapper.login(user.getAccount());
            if(user1.getRegisterStatus()==1){
                return 0;//返回账号已经被注册
            } else {//不为1则为0，表示账号在注册申请中
                return 2;//账号正在申请中
            }
        } else {//执行注册
            userMapper.register(user.getAccount(), user.getPassword(), user.getName(), user.getClassName(),new Date());
            return 1;
        }
    }

    @Override
    public User searchByAccount(String account) {
        return userMapper.login(account);
    }

    @Override
    public void deleteUserById(Integer id) {
        userMapper.deleteUserById(id);//删除用户
        applyRecordMapper.deleteApplyRecordByUserId(id);//删除该用户的申请素拓记录
        activityRecordMapper.deleteActivityRecordByUserId(id);//删除该用户的活动记录
        emailRecordMapper.deleteAllEmailRecordsByUserId(id);//删除该用户的信件记录
        summaryMapper.deleteAllSummariesByUserId(id);//删除其所有心得记录
    }

    @Override
    public void changeUserInfo(User user) {
        userMapper.changeUserInfo(user.getName(),user.getClassName(),user.getId());
    }

    @Override
    public User[] getUserApplies(Integer start) {
        return userMapper.getUserApplies(start);
    }

    @Override
    public void agreeRegister(Integer id) {//通过某位学生的注册申请
        userMapper.agreeRegister(id);
    }

    @Override
    public void disagreeRegister(Integer id) {//拒绝某位学生的注册申请，无需执行其他操作
        userMapper.disagreeRegister(id);
    }

    @Override
    public Integer changePassword(String oldPassword, String newPassword, Integer id) {
        User user=userMapper.beforeChangePassword(oldPassword,id);//获取学生
        if(user==null){//获取到的是空则表明旧密码校验失败
            return 0;//旧密码校验失败
        } else {
            Integer result= userMapper.changePassword(newPassword,id);
            if(result==1){//密码修改成功
                return 1;
            } else {
                return 2;//出现数据库更新错误
            }
        }
    }
}
